/**
 * Created by Jimmy on 7/2/2015.
 */

package fyp.spedmew.util;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import fyp.spedmew.backend.entity.jobEndpoint.model.Address;
import fyp.spedmew.backend.entity.jobEndpoint.model.Job;
import fyp.spedmew.backend.entity.userEndpoint.model.User;
import fyp.spedmew.category.UserType;

public class Validator {

    public static boolean checkInt(String input) {
        int temp = 0;
        boolean valid = false;

        try {
            temp = Integer.parseInt(input);
            valid = true; }
        catch (NumberFormatException e) {}

        return valid;
    }

    // Check length of string > 5
    public static boolean checkPWLength(String input) {
        return (input.length() > 6);
    }

    // Check if string contains a digit, uppercase and lowercase letter
    public static boolean checkPWChar(String input) {
        if (Pattern.compile("[0-9]").matcher(input).find() &&
            Pattern.compile("[A-Z]").matcher(input).find() &&
            Pattern.compile("[a-z]").matcher(input).find())
        { return true; }

        return false;
    }

    // Check a string for whitespace
    public static boolean checkStrSpace(String input) {
        Pattern pattern = Pattern.compile("\\s");
        Matcher matcher = pattern.matcher(input);
        boolean found = matcher.find();

        return found;
    }

    public static char checkUser(User user) {

        if(checkStrSpace(user.getName()) ||
           user.getName().isEmpty()) {
            return 'A';
        }

        if(user.getPhone().isEmpty()) {
            return 'B';
        }

        if(!checkPWLength(user.getPassword()) ||
           !checkPWChar(user.getPassword()) ||
           checkStrSpace(user.getPassword()))
                return 'C';

        if(user.getType() != UserType.USER &&
           user.getType() != UserType.DISPATCHER)
            return  'D';

        return 'P';
    }

    public static char checkJob(Job job) {

        if(Integer.parseInt(job.getPrice()) <= 0)
            return 'A';

        if(TextUtils.isEmpty(job.getTitle()))
            return 'B';

        if(TextUtils.isEmpty(job.getDetail()))
            return 'C';

        Address temp = job.getDeliverAddress();
        if(TextUtils.isEmpty(temp.getLine1()))
            return 'D';

        temp = job.getPickupAddress();
        if(TextUtils.isEmpty(temp.getLine1()))
            return 'E';

        return 'P';
    }
}
